home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_441 / dme / src / rexx.c.mmw < prev    next >
Text File  |  1992-05-06  |  8KB  |  280 lines

  1.  
  2. /*
  3.  *  REXX.C
  4.  *
  5.  *      (c) Copyright 1987 by Kim DeVaughn, All Rights Reserved
  6.  *
  7.  *  ARexx interface code, etc.
  8.  *
  9.  */
  10.  
  11. #include "defs.h"
  12. #include "rexx.h"
  13. #include <stdio.h>
  14.  
  15. #if AREXX
  16.  
  17. int foundcmd;       /* control for implicit ARexx macro invocation   */
  18. int cmderr;         /* global command error flag for do_rexx()'s use */
  19.  
  20. __stdargs APTR CreateRexxMsg ARGS((PORT *, char *, char *));
  21. __stdargs void DeleteRexxMsg ARGS((struct RexxMsg *));
  22. __stdargs APTR CreateArgstring ARGS((char *, int));
  23. __stdargs void DeleteArgstring ARGS((struct RexxArg *));
  24. __stdargs int IsRexxMsg ARGS((struct RexxMsg *));
  25. __stdargs void InitPort ARGS((PORT *, char *));
  26. __stdargs void FreePort ARGS((PORT *));
  27.  
  28. struct RxsLib *RexxSysBase;
  29.  
  30. typedef struct Message MSG;
  31.  
  32. /*
  33.  * initialization for ARexx ... just open rexsyslib.library
  34.  */
  35.  
  36. void
  37. openrexx()
  38. {
  39.     RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", (ULONG)RXSVERS);
  40.     return;
  41. }
  42.  
  43.  
  44.  
  45. /*
  46.  * cleanup any open ARexx stuff ...  just close rexsyslib.library for now
  47.  */
  48.  
  49. void
  50. closerexx()
  51. {
  52.     if (RexxSysBase)
  53.         CloseLibrary((struct Library *)RexxSysBase);
  54. }
  55.  
  56.  
  57.  
  58. /*
  59.  *  explicit invocation interface between do_command() and do_rexx
  60.  *  for ARexx macros having NO arguments (i.e., for the "rx" command)
  61.  */
  62.  
  63. void
  64. do_rx()
  65. {
  66.     do_rexx(av[1]);
  67. }
  68.  
  69.  
  70.  
  71. /*
  72.  *  explicit invocation interface between do_command() and do_rexx
  73.  *  for ARexx macros having ONE argument (i.e., for the "rx1" command)
  74.  */
  75.  
  76. void
  77. do_rx1()
  78. {
  79.     char macbuf[256];
  80.  
  81.     strcpy(macbuf, av[1]);
  82.     strcat(macbuf, " ");
  83.     strcat(macbuf, av[2]);
  84.     do_rexx(macbuf);
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.  *  explicit invocation interface between do_command() and do_rexx
  91.  *  for ARexx macros having TWO arguments (i.e., for the "rx2" command)
  92.  */
  93.  
  94. void
  95. do_rx2()
  96. {
  97.     char macbuf[256];
  98.  
  99.     strcpy(macbuf, av[1]);
  100.     strcat(macbuf, " ");
  101.     strcat(macbuf, av[2]);
  102.     strcat(macbuf, " ");
  103.     strcat(macbuf, av[3]);
  104.     do_rexx(macbuf);
  105. }
  106.  
  107.  
  108.  
  109. /*
  110.  *  implicit invocation interface between do_command() and do_rexx
  111.  *  for ARexx macros implicitly called; arbitrary number of arguments
  112.  */
  113.  
  114. void
  115. do_rxImplied(cmd, args)
  116. char *cmd;
  117. char *args;
  118. {
  119.     char macbuf[256];
  120.  
  121.     strcpy(macbuf, cmd);
  122.     strcat(macbuf, " ");
  123.     strcat(macbuf, args);
  124.     do_rexx(macbuf);
  125. }
  126.  
  127. /*
  128.  *  issue a command to ARexx ...
  129.  */
  130.  
  131. int
  132. do_rexx(macstr)
  133. char *macstr;
  134. {
  135.     struct RexxArg *macarg;
  136.  
  137.     struct MsgPort  RexxPort;
  138.     struct MsgPort *ARexxPort;
  139.  
  140.     struct RexxMsg *macptr;
  141.     struct RexxMsg *cmdptr;
  142.  
  143.     char host[16];
  144.     char hexbuf[12];        /* should only need 9 bytes */
  145.     char errmsg[80];        /* don't build a larger error message */
  146.  
  147.     int  ret;
  148.     int  err;
  149.  
  150.     long oldlock;                           /* MMW */
  151.  
  152.     char buf[256], *arg0, *arg, *str;     /* MMW */
  153.     char quoted, *aux;                   /* MMW */
  154.     short i;                            /* MMW */
  155.  
  156.  
  157.  
  158.     if (RexxSysBase == 0) {
  159.         title("Unknown Command   -   No Macros:  ARexx Not Installed ");   /* no rexxsyslib */
  160.         return(0);
  161.     }
  162.  
  163.     BZero(&RexxPort, sizeof(struct MsgPort));
  164.     strcpy(host, "DME");
  165.     sprintf(hexbuf, "%08x", &RexxPort);
  166.     strcat(host, hexbuf);
  167.     InitPort(&RexxPort, host);      /* need to error check this */
  168.     AddPort(&RexxPort);
  169.     /* return here if InitPort failed */
  170.  
  171.  
  172.     if (macarg = (struct RexxArg *)CreateArgstring(macstr, strlen(macstr))) {
  173.         if (macptr = (struct RexxMsg *)CreateRexxMsg(&RexxPort, "dme", host)) {
  174.             ACTION(macptr) = RXCOMM;
  175.             ARG0(macptr)   = (STRPTR)macarg;
  176.  
  177.             oldlock = CurrentDir(Ep->dirlock);                      /* MMW */
  178.  
  179.             Forbid();
  180.             if (ARexxPort = (struct MsgPort *)FindPort("REXX")) {
  181.                 PutMsg(ARexxPort, (MSG *)macptr);
  182.                 Permit();
  183.  
  184. /*                title("Calling ARexx Macro ... ");    */      /* MMW */
  185.  
  186.                 for (;;) {
  187.                     WaitPort(&RexxPort);
  188.                     cmdptr = (struct RexxMsg *)GetMsg(&RexxPort);
  189.     /* MMW (start) */
  190.                     if (IsRexxMsg(cmdptr)) {
  191.                         foundcmd = 0;
  192.                         cmderr = CMD_INITIAL;
  193.                         RESULT1(cmdptr) = RESULT2(cmdptr) = 0;
  194.  
  195.                         arg0 = ARG0(cmdptr);
  196.                         for (i = 0; i < 20 && arg0[i] > ' ' ; i++) {
  197.                             buf[i] = (arg0[i] >= 'A' && arg0[i] <= 'Z') ? arg0[i] + ('a' - 'A') : arg0[i];
  198.                         }
  199.                         buf[i] = 0;
  200.                         if (strcmp(buf, "getval") == 0) {
  201.                             foundcmd = 1;
  202.                             strncpy(buf, arg0 + 6, 255),
  203.                             buf[255] = 0;
  204.                             if (cmdptr->rm_Action & RXFF_RESULT) {
  205.                                 str = buf;
  206.                                 if (arg = breakout(&str, "ed, &aux))
  207.                                     RESULT2(cmdptr) = (long) CreateArgstring(arg, strlen(arg));
  208.                                 if (aux)
  209.                                     free(aux);
  210.                             }
  211.                         }
  212.  
  213.                         else {
  214.                             strncpy(buf, ARG0(cmdptr), 255);
  215.                             buf[255] = 0;
  216.                             ret = do_command(buf);
  217.                             err = cmderr;
  218.  
  219.     /* MMW (end) */
  220.  
  221.                             if (foundcmd) {
  222.                                 ret = (ret == 1) ? 0 : 5;   /* cmd error:  RC = 5  */
  223.                             } else {
  224.                                 ret = do_rexx(buf);    /* another macro? */
  225.                             }
  226.                             RESULT1(cmdptr) = ret;
  227.                         }
  228.  
  229.                         ReplyMsg((MSG *)cmdptr);
  230.                     }
  231.                     do_command("null");     /* a kludge to set "foundcmd" */
  232.                     if (macptr == cmdptr) break;
  233.                 }
  234.  
  235.  
  236.                 if (ret = RESULT1(cmdptr)) {
  237.                     if (RESULT2(cmdptr)) {
  238.                         if (RESULT2(cmdptr) == 1) {
  239.                             title("Unknown Command ");
  240.                         } else {
  241.                             sprintf(errmsg, "ARexx Macro Error:  Code = %d  Severity = %d ", RESULT2(cmdptr), ret);
  242.                             title(errmsg);
  243.                         }
  244.                     } else {
  245.                         sprintf(errmsg, "User Specified Macro Error:  RC = %d ", ret);
  246.                         title(errmsg);
  247.                     }
  248.                 } else {
  249. /*                    if (err <= TITLE_THRESHHOLD) {    */          /* MMW */
  250. /*                        title("OK ");                 */         /* MMW */
  251. /*                    }                                 */        /* MMW */
  252.                 }
  253.                 ret = ret + err;
  254.             } else {
  255.                 Permit();
  256.                 title("Unknown Command   -   No Macros:  ARexx Not Active ");   /* no REXX port */
  257.  
  258.                 ret = -1;
  259.             }
  260.             CurrentDir(oldlock);                    /* MMW */
  261.  
  262.             DeleteRexxMsg(macptr);
  263.         } else {
  264.             title("CreateRexxMsg() Failed ");   /* may be overkill, and not need to ckeck this */
  265.             ret = -1;
  266.         }
  267.         DeleteArgstring(macarg);
  268.     } else {
  269.         title("CreateArgstring() Failed ");     /* may be overkill, and not need to check this */
  270.         ret = -1;
  271.     }
  272.  
  273.     RemPort(&RexxPort);
  274.     FreePort(&RexxPort);
  275.     return(ret);
  276. }
  277.  
  278. #endif
  279.  
  280.